import Vue from 'vue'
import axios from 'axios'

Vue.prototype.$axios = axios

//  请求携带cookie
axios.defaults.withCredentials = true

// 请求拦截器
axios.interceptors.request.use(config => config)

// 响应拦截器
axios.interceptors.response.use(response => response.data, error => {
  if (error.response) {
    error = error.response
    let status = error.status || 0
    switch (status) {
      case 301:
        console.log('请登录')
        break
      case 404:
        console.log('404')
        break
    }
  }
  console.log(error)
  return Promise.reject(error.data || error)
})

/**
 * 下载音乐
 * @param url
 * @returns {Promise<AxiosResponse<any>>}
 */
export function download (url) {
  return axios.get(url, {responseType: 'blob'})
}
